package com.lei.demo.controller;

import com.lei.demo.entity.TotalForm;
import com.lei.demo.entity.User;
import com.lei.demo.service.TotalFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Shinelon on 2017/6/15.
 */
@Controller
@RequestMapping("/totalform")
public class TotalFormController {

    @Autowired
    TotalFormService totalFormService;

    @RequestMapping(value = "/handup", method = {RequestMethod.GET, RequestMethod.POST})
    public String handUpTotalForm(Model model, TotalForm totalForm, HttpServletRequest request) {
    	model.addAttribute("status","exist");
    	User user = (User) request.getSession().getAttribute("user");
        totalForm.setUsername(user.getName());
        totalFormService.save(totalForm);
        model.addAttribute("saved", "success");
        return "TotalForm_new";
    }

    @RequestMapping(value = "/increase", method = {RequestMethod.GET, RequestMethod.POST})
    public String increaseTotalForm(Model model, TotalForm totalForm) {
        model.addAttribute("status", "exist");
        return "TotalForm_new";
    }

    @RequestMapping(value = "/budget", method = {RequestMethod.GET, RequestMethod.POST})
    public String budget(Model model, TotalForm totalForm) {
        model.addAttribute("status", "exist");
        return "budget_new";
    }

    @RequestMapping(value = "/total", method = {RequestMethod.GET, RequestMethod.POST})
    public String totalForm(Model model, TotalForm totalForm) {
        model.addAttribute("status", "exist");
        return "TotalForm_new";
    }

    @RequestMapping(value = "/trip", method = {RequestMethod.GET, RequestMethod.POST})
    public String tripForm(Model model, TotalForm totalForm) {
        return "TripForm_new";
    }

    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public String updateTotalForm(Model model, TotalForm totalForm) {
        totalFormService.update(totalForm);
        model.addAttribute("saved", "success");
        return "TotalForm_edit";
    }

    @RequestMapping(value = "/check", method = {RequestMethod.GET, RequestMethod.POST})
    public String checkTotalForm(Model model, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        List<TotalForm> totalForms = totalFormService.findTotalForms(user.getName());
        model.addAttribute("totfs", totalForms);
        return "TotalForm_check";
    }

    @RequestMapping(value = "delete/{toid}", method = {RequestMethod.GET, RequestMethod.POST})
    public String deleteTotalForm(Model model, @PathVariable int toid) {
        List<TotalForm> totalForms = totalFormService.findTotalForms(toid);
        model.addAttribute("totalForms", totalForms);
        totalFormService.delete(toid);
        model.addAttribute("delete", "success");
        model.addAttribute("totalForm", new TotalForm());
        return "TotalForm_check";
    }

    @RequestMapping(value = "edit/{toid}", method = {RequestMethod.GET,RequestMethod.POST})
    public String updateTripForm(Model model, @PathVariable int toid) {
        TotalForm totalForm = totalFormService.findById(toid);
        model.addAttribute("totalform", totalForm);
        return "TotalForm_edit";
    }
}